package com.vivid.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.vivid.system.domain.User;
import com.vivid.system.mapper.PredictionMapper;
import com.vivid.system.domain.Prediction;
import com.vivid.system.service.IPredictionService;

/**
 * 预测表Service业务层处理
 *
 * @author vivid
 * @date 2022-08-09
 */
@Service
public class PredictionServiceImpl implements IPredictionService
{
    @Autowired
    private PredictionMapper predictionMapper;

    /**
     * 查询预测表
     *
     * @param predictionId 预测表主键
     * @return 预测表
     */
    @Override
    public Prediction selectPredictionByPredictionId(Long predictionId)
    {
        return predictionMapper.selectPredictionByPredictionId(predictionId);
    }

    /**
     * 查询预测表列表
     *
     * @param prediction 预测表
     * @return 预测表
     */
    @Override
    public List<Prediction> selectPredictionList(Prediction prediction)
    {
        return predictionMapper.selectPredictionList(prediction);
    }

    /**
     * 新增预测表
     *
     * @param prediction 预测表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPrediction(Prediction prediction)
    {
        int rows = predictionMapper.insertPrediction(prediction);
        insertUser(prediction);
        return rows;
    }

    /**
     * 修改预测表
     *
     * @param prediction 预测表
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePrediction(Prediction prediction)
    {
        predictionMapper.deleteUserByUserId(prediction.getPredictionId());
        insertUser(prediction);
        return predictionMapper.updatePrediction(prediction);
    }

    /**
     * 批量删除预测表
     *
     * @param predictionIds 需要删除的预测表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePredictionByPredictionIds(Long[] predictionIds)
    {
        predictionMapper.deleteUserByUserIds(predictionIds);
        return predictionMapper.deletePredictionByPredictionIds(predictionIds);
    }

    /**
     * 删除预测表信息
     *
     * @param predictionId 预测表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePredictionByPredictionId(Long predictionId)
    {
        predictionMapper.deleteUserByUserId(predictionId);
        return predictionMapper.deletePredictionByPredictionId(predictionId);
    }

    /**
     * 通过userId查找prediction表
     * @param uesrId
     * @return
     */
    @Override
    public List<Prediction> selectPredictionByUserId(Long uesrId) {
        return predictionMapper.selectPredictionByUserId(uesrId);
    }

    /**
     * 新增用户信息管理信息
     *
     * @param prediction 预测表对象
     */
    public void insertUser(Prediction prediction)
    {
        List<User> userList = prediction.getUserList();
        Long predictionId = prediction.getPredictionId();
        if (StringUtils.isNotNull(userList))
        {
            List<User> list = new ArrayList<User>();
            for (User user : userList)
            {
                user.setUserId(predictionId);
                list.add(user);
            }
            if (list.size() > 0)
            {
                predictionMapper.batchUser(list);
            }
        }
    }

}
